<template>
	<view class="login">
		<view class="title">
			We Are Family
		</view>
		<u--form labelPosition="left" :model="user" :rules="rules" labelWidth="100" ref="uForm" borderBottom>
			<u-form-item label="用户名" prop="uname">
				<u--input v-model="user.uname" border="none" placeholder="请输入用户名"></u--input>
			</u-form-item>
			<u-form-item label="密码" prop="upwd">
				<u--input v-model="user.upwd" type="password" border="none" placeholder="请输入密码"></u--input>
			</u-form-item>
			<view style="padding: 20rpx 0;">
				<checkbox-group @change="checked = !checked">
					<label>
						<checkbox color="#3c9cff" :checked="checked"/><text>记住密码</text>
					</label>
				</checkbox-group>
			</view>
			<u-form-item labelWidth="0">
				<u-button type="primary" @click="login">登录</u-button>
			</u-form-item>
		</u--form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checked: true,
				user: {
					uname: uni.getStorageSync('uname') || "",
					upwd: uni.getStorageSync('upwd') || ""
				},
				rules: {
					'uname': {
						type: 'string',
						required: true,
						message: '请填写用户名称',
						trigger: ['blur', 'change']
					},
					'upwd': {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur', 'change']
					}
				},
			};
		},
		methods: {
			login() {
				this.$refs.uForm.validate().then(res => {
					if(this.checked){
						uni.setStorage({
							key:"uname",
							data:this.user.uname
						})
						uni.setStorage({
							key:"upwd",
							data:this.user.upwd
						})
					}
					uniCloud.callFunction({
						name: 'login',
						data: {
							uname: this.user.uname,
							upwd: this.user.upwd
						},
						success(res) {
							console.log(res)
							if (res.result.code == 200) {
								uni.reLaunch({
									url: '../home/home'
								})
							} else {
								uni.showToast({
									title: res.result.msg,
									icon: 'none'
								})
							}
						}
					})
				}).catch(errors => {})
			}
		},
	};
</script>
<style lang="scss" scoped>
	.login {
		box-sizing: border-box;
		padding: 25rpx;

		.title {
			display: flex;
			justify-content: center;
			padding: 50rpx 0;
			font-size: 50rpx;
			font-weight: 700;
		}
	}
</style>
